﻿using System;
using System.Text.RegularExpressions;
using System.Text;
namespace System
{
    /// <summary>
    /// 字符串转换类
    /// </summary>
    public static class StringConvert
    {
        /// <summary>
        /// 截取字符串函数
        /// </summary>
        /// <param name="str">所要截取的字符串</param>
        /// <param name="num">截取字符串的长度</param>
        /// <param name="flg">true:加...,flase:不加</param>
        /// <returns></returns>
        public static string CutString(this string str, int num, bool flg)
        {
            #region
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(str);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }

                try
                {
                    tempString += str.Substring(i, 1);
                }
                catch
                {
                    break;
                }

                if (tempLen > num)
                    break;
            }
            //如果截过则加上半个省略号
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(str);
            if (mybyte.Length > num)
                if (flg)
                {
                    tempString += "...";
                }
            return tempString;
            #endregion
        }
        /// <summary>
        /// 字符串中是否含有数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool HasNumber(this string str)
        {
            bool result = false;
            for (int i = 0; i < str.Length; i++)
            {
                if (Char.IsNumber(str, i))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
        /// <summary>
        /// 去掉字符串中的数字
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string RemoveNumber(this string key)
        {
            return System.Text.RegularExpressions.Regex.Replace(key, @"\d", "");
        }
        /// <summary>
        /// 将数字转换成为1,000,000.00形式
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetMoneyString(this string num)
        {
            return string.Format("{0:N2}", num);
        }

        /// <summary>
        /// 过滤输入的危险信息
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>
        public static string FilteT_SQL(this string strtb, int maxLength)
        {
            #region
            strtb = strtb.Trim();
            if (string.IsNullOrEmpty(strtb))
                return string.Empty;
            if (strtb.Length > maxLength)
                strtb = strtb.Substring(0, maxLength);
            strtb = strtb.Replace("!", "");
            strtb = strtb.Replace("@", "");
            strtb = strtb.Replace("#", "");
            strtb = strtb.Replace("$", "");
            strtb = strtb.Replace("%", "");
            strtb = strtb.Replace("^", "");
            strtb = strtb.Replace("&", "");
            strtb = strtb.Replace("*", "");
            strtb = strtb.Replace("(", "");
            strtb = strtb.Replace(")", "");
            strtb = strtb.Replace("_", "");
            strtb = strtb.Replace("+", "");
            strtb = strtb.Replace("|", "");
            strtb = strtb.Replace("?", "");
            strtb = strtb.Replace("/", "");
            strtb = strtb.Replace(".", "");
            strtb = strtb.Replace(">", "");
            strtb = strtb.Replace("<", "");
            strtb = strtb.Replace("{", "");
            strtb = strtb.Replace("}", "");
            strtb = strtb.Replace("[", "");
            strtb = strtb.Replace("]", "");
            strtb = strtb.Replace("-", "");
            strtb = strtb.Replace("=", "");
            strtb = strtb.Replace(",", "");
            strtb = strtb.Replace("`", "");
            strtb = strtb.Replace("~", "");
            strtb = strtb.Replace("'", "");
            strtb = strtb.Replace(":", "");
            strtb = strtb.Replace(";", "");
            strtb = Regex.Replace(strtb, "(\\s*[s|S][e|E][l|L][e|E][c|C][t|T]\\s*)+", "");
            strtb = Regex.Replace(strtb, "(\\s*[i|I][n|N][s|S][e|E][r|R][t|T]\\s*)+", "");
            strtb = Regex.Replace(strtb, "(\\s*[d|D][e|E][l|L][e|E][t|T][e|E]\\s*)+", "");
            strtb = Regex.Replace(strtb, "(\\s*[u|U][p|P][d|D][a|A][t|T][a|A]\\s*)+", "");
            strtb = Regex.Replace(strtb, "(\\s*[d|D][r|R][o|O][p|P]\\s*)+", "");
            return strtb;
            #endregion
        }
        /// <summary>
        /// 判断输入是否数字
        /// </summary>
        /// <param name="num">要判断的字符串</param>
        /// <returns></returns>
        /// <summary>
        public static bool IsInt(this string num)
        {
            #region
            try
            {
                Convert.ToInt32(num);
                return true;
            }
            catch { return false; }
            #endregion
        }
        /// <summary>
        /// 判断一个字符串是否是时间格式
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static bool IsDatetime(this string strValue)
        {
            #region
            string strReg = @"([1-2][0-9][0-9][0-9])-(0*[1-9]|1[0-2])-(0*[1-9]|[12][0-9]|3[01])\ (0*[0-9]|1[0-9]|2[0-3]):(0*[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])";
            if (strValue == "")
            {
                return false;
            }
            else
            {
                Regex re = new Regex(strReg);
                MatchCollection mc = re.Matches(strValue);
                if (mc.Count == 1)
                    foreach (Match m in mc)
                    {
                        if (m.Value == strValue)
                            return true;
                    }
            }
            return false;
            #endregion
        }
        /// <summary>
        /// 判断输入是否中文
        /// </summary>
        /// <param name="CString">要判断的字符串</param>
        /// <returns></returns>
        public static bool IsChinaWords(this string str)
        {
            #region
            bool BoolValue = false;
            for (int i = 0; i < str.Length; i++)
            {
                if (Convert.ToInt32(Convert.ToChar(str.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
                {
                    BoolValue = false;
                }
                else
                {
                    BoolValue = true;
                }
            }
            return BoolValue;
            #endregion
        }
        /// <summary>
        /// 字符串转换成INT
        /// </summary>
        /// <param name="str">要转换成int的字符传</param>
        /// <returns>返回0就是错误</returns>
        public static int GetStrToInt(this string str)
        {
            #region
            try
            {
                if (str.Length == 0)
                    return 0;
                else
                {
                    if (Int32.Parse(str) == 0)
                        return 0;
                    else
                        return Int32.Parse(str);
                }
            }
            catch
            {
                return 0;
            }
            #endregion
        }
        /// <summary>
        /// 对时间进行格式化，如：2007-1-15,2007/5/2
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="geshi">如：y-m-d；y/m/d；y-m-d h:mm:ss；m-d-y；m/d/y</param>
        /// <param name="spstr">分隔符号，如：-，/</param>
        /// <returns></returns>
        public static string GetDateToString(this DateTime dt, string geshi, string spstr)
        {
            #region
            string str = "";
            string y, m, d, h, mm, ss;
            y = dt.Year.ToString();
            m = dt.Month.ToString();
            if (m.Length < 2) m = "0" + m;
            d = dt.Day.ToString();
            if (d.Length < 2) d = "0" + d;
            h = dt.Hour.ToString();
            if (h.Length < 2) h = "0" + h;
            mm = dt.Minute.ToString();
            if (mm.Length < 2) mm = "0" + mm;
            ss = dt.Second.ToString();
            if (ss.Length < 2) ss = "0" + ss;

            if (geshi == "y-m-d")
            {
                str = y + spstr + m + spstr + d;
            }
            else if (geshi == "y-m-d h:mm:ss")
            {
                str = y + spstr + m + spstr + d + " " + h + ":" + mm + ":" + ss;
            }
            else if (geshi == "m-d-y")
            {
                str = m + spstr + d + spstr + y;
            }
            else if (geshi == "d-m-y")
            {
                str = d + spstr + m + spstr + y;
            }
            else
            {
                str = DateTime.Now.ToString();
            }
            return str;
            #endregion
        }
        /// <summary>
        /// 对表单内容进行转换HTML操作, 
        /// </summary>
        /// <param name="fString">html字符串</param>
        /// <returns></returns>
        static public string HtmlDiscode(this string theString)
        {
            #region
            if (theString != "")
            {
                theString = theString.Replace("&gt;", ">");
                theString = theString.Replace("&lt;", "<");
                theString = theString.Replace("&nbsp;", " ");
                theString = theString.Replace(" &nbsp;", "  ");
                theString = theString.Replace("&quot;", "\"");
                theString = theString.Replace("&#39;", "\'");
                theString = theString.Replace("<br/> ", "\n");
                return theString;
            }
            else
                return string.Empty;
            #endregion
        }
        /// <summary>
        /// 对表单内容进行转换HTML操作
        /// </summary>
        /// <param name="fString">html字符串</param>
        /// <returns></returns>
        static public string HtmlEncode(this string theString)
        {
            #region
            if (theString != "")
            {
                theString = theString.Replace(">", "&gt;");
                theString = theString.Replace("<", "&lt;");
                theString = theString.Replace("  ", " &nbsp;");
                theString = theString.Replace("  ", " &nbsp;");
                theString = theString.Replace("\"", "&quot;");
                theString = theString.Replace("\'", "&#39;");
                theString = theString.Replace("\n", "<br/> ");
                return theString;
            }
            else
                return string.Empty;
            #endregion
        }
        /// <summary>
        /// 得到没有HTML标签的网页文本
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetNoHTMLText(this string str)
        {
            string html = str;

            html = Regex.Replace(html, @"<!--[\s\S]*?-->", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<[//]*tr[^>]*>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<[//]*p[^>]*>", "\n", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<[//]*br[^>]*>", "\n", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<[//]*div[^>]*>", "\n", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<STYLE[\s\S]*?</STYLE>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<script[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<[\?!A-Za-z/][^><]*>", "");
            html = Regex.Replace(html, "\r", "");
            html = Regex.Replace(html, "\n", "\r\n");
            html = Regex.Replace(html, @"[　|\s]*\r\n[　|\s]*\r\n", "\r\n");
            //html = Regex.Replace(html,@"(\r\n)[^ 　]","$1");
            html = formatHtml(html);
            return (html);
        }
        private static string formatHtml(string str)
        {
            //替换<P>
            string html = Regex.Replace(str, " ", " ");
            html = Regex.Replace(html, "&nbsp;", " ");
            html = Regex.Replace(html, "&nbsp", " ");
            html = Regex.Replace(html, "&#8226;", " ");
            html = Regex.Replace(html, "&#8226", " ");
            html = Regex.Replace(html, "&#146;", "'");
            html = Regex.Replace(html, "&#147;", "“");
            html = Regex.Replace(html, "&#148;", "”");
            html = Regex.Replace(html, "&#160;", "");
            html = Regex.Replace(html, "&amp;", "&");
            html = Regex.Replace(html, "&copy;", "@");
            html = Regex.Replace(html, "&#150;", "–");
            html = Regex.Replace(html, "&quot;", " ");
            html = Regex.Replace(html, "&lt;", " ");
            html = Regex.Replace(html, "&gt;", " ");
            html = Regex.Replace(html, "&#13;&#10;", "");
            return html;
        }
    }
}
